/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_A5_phi_vs_z_country.do
Date: October 2022

Description: Reproduces Table A.5: Output per-worker and firm-embedded productivity, by country.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"


*Log
cap log close
log using "${clogs}/table_A5_phi_vs_z_country.log",replace
*-------------------------------------------------------------------------------




*Aggregate economy 
*-----------------------------------------------------------------
clear all
use "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

keep isocode D_A_phi ln_gdp_pw_ppp
rename D_A_phi delta_phi_agg
rename ln_gdp_pw_ppp delta_yn
format %9.2fc delta_phi_agg delta_yn
sort isocode

tempfile temp_agg
save `temp_agg', replace 
	
	
	

*Manufacturing and Services 
*-----------------------------------------------------------------
clear all
use "${data}/estimates_aggtype_naics_sales_s1_base_woparent.dta", clear

local grvar0 "LP_ppp_emp_pwt" 
local grvar1 ""	
local rr ""
local var3 "A"
local yy "2016"
local yy_ref=`yy'	
replace year=`yy_ref'
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
*---------------------------------------
gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
}

tempfile temp
save `temp', replace


*Manufacturing 
*-------------------------------------------------------------------------------------
use `temp', clear
keep if type=="manuf"
keep isocode D_A_phi 
rename D_A_phi delta_phi_manuf
format %9.2fc delta_phi_manuf 
sort isocode

tempfile temp_manuf
save `temp_manuf', replace 


*Services 
*-------------------------------------------------------------------------------------
use `temp', clear
keep if type=="serv"
keep isocode D_A_phi 
rename D_A_phi delta_phi_serv
format %9.2fc delta_phi_serv 
sort isocode

tempfile temp_serv
save `temp_serv', replace 




*Merging PHI's aggregate, manufacturing, and services 
*-------------------------------------------------------------------------------------
use `temp_agg', clear

merge m:1 isocode using `temp_manuf'
drop _merge

merge m:1 isocode using `temp_serv'
drop _merge

export delimited using "${rappendix}/table_A5_phi_vs_z_country.csv", datafmt replace



